#!/bin/bash

##########################################
#        author:     韩加敏              #
#        date:       2019-03-18:v2.0     #
#        version:    2019-06-16:v3.0     #
#        function:   自动化安装oracle    #
##########################################

#使用 root 用户执行此脚本

#请指定centos版本,输入6/7:
CENTOS_VERSION=7
#请指定oracle的版本,输入11/12:
ORACLE_VERSION=12
#请定义服务器IP:
SERVER_IP=192.168.100.201
#请定义oracle_base目录:
ORACLE_BASE=/u01/app/oracle
#请定义oracle安装包位置:
SOFTWARE_DIR=/u01/software
#请定义oracle用户名和密码:
ORACLE_USERNAME=oracle
ORACLE_PASSWD=H@nd1234
#请定义SID和GLOBAL_NAME:
ORACLE_SID=SRM
GLOBAL_DB_NAME=SRM
#请定义SGA和PGA:
INSTANCE_SGA=1200M
INSTANCE_PGA=256M




#可以开始执行脚本进行安装！！！！！！！！！！！！！！！！！！！！

echo -e "\n***********************    oracle    start   install   ***********************\n"
echo "
##################################################
#        author:       hanjiamin                 #
#        date:         2019-03-18 2.0            #
#        version:      2019-06-16 3.0            #
#        function:     auto install oracle       #
##################################################
"
export CENTOS_VERSION
export ORACLE_VERSION
export SERVER_IP
export ORACLE_BASE
export SOFTWARE_DIR
export ORACLE_USERNAME
export ORACLE_PASSWD
export ORACLE_SID
export GLOBAL_DB_NAME
export INSTANCE_SGA
export INSTANCE_PGA

#准备yum源和安装模板、响应文件
function ready_yum_rsp_dbt_file() {
#判断目标文件夹是否存在，不存在直接退出
if [ ! -d ${SOFTWARE_DIR} ];then
        echo "${SOFTWARE_DIR} 目标文件夹不存在，请查看，并重新运行安装脚本"
		exit 1
fi

tar -zxf ${SOFTWARE_DIR}/install_necessary_oracle_file.tgz -C ${SOFTWARE_DIR}/
}

#安装依赖
function install_dependent() {
echo -e "\n开始检查网络环境！\n"
if [ "$(curl -I -m 10 -o /dev/null -s -w %{http_code}  www.baidu.com)" -eq 200 ]
        then
            echo -e "网络连接正常，状态码:200\n"
		echo -e "开始配置系统环境！\n"
		cd /etc/yum.repos.d
		tar -zcvf backup_repo.tgz *.repo &>/dev/null
		rm -fr *.repo
		cp ${SOFTWARE_DIR}/CentOS6-Base.repo /etc/yum.repos.d/CentOS-Base.repo
		if [ "${CENTOS_VERSION}" -eq 7 ];then
			sed -i "s#6#7#g" /etc/yum.repos.d/CentOS-Base.repo
		fi
        yum -y install binutils compat-libcap1 compat-libstdc++-33 elfutils-libelf elfutils-libelf-devel gcc gcc-c++ glibc glibc-common glibc-devel glibc-headers kernel-headers ksh libaio  libaio-devel libgcc libgomp libstdc++ libstdc++-devel make numactl numactl-devel sysstat unixODBC unixODBC-devel unzip bc &>/tmp/oracle_dependcy_output_detail_$(date +"%Y_%m_%d").file
		rm -fr CentOS-Base.repo
		tar -zxvf backup_repo.tgz &>/dev/null
		rm -fr backup_repo.tgz
		rm -f /tmp/oracle_dependcy_output_detail_$(date +"%Y_%m_%d").file
		echo -e "依赖环境配置完毕！\n"	
else
                echo -e "对不起，网络连接失败！\n"
		exit

fi
}

#修改系统内核参数
function modify_kernel_parameter() {
#先删除最大共享内存和内存页，然后添加需要的设置值
#根据内存计算值
echo -e "开始修改内核参数！\n"
MEMORY=$(free -m | grep "Mem:" | awk '{print $2}')
KERNEL_SHMMAX=$(($MEMORY*1024*1024-1))
KERNEL_SHMALL=$(($MEMORY*1024/4))
sed -i '/kernel.shmmax/d' /etc/sysctl.conf
sed -i '/kernel.shmall/d' /etc/sysctl.conf
sed -i '/fs.aio-max-nr/d' /etc/sysctl.conf
sed -i '/fs.file-max/d' /etc/sysctl.conf
sed -i '/kernel.shmmni/d' /etc/sysctl.conf
sed -i '/kernel.sem/d' /etc/sysctl.conf
sed -i '/net.ipv4.ip_local_port_range/d' /etc/sysctl.conf
sed -i '/net.core.rmem_default/d' /etc/sysctl.conf
sed -i '/net.core.rmem_max/d' /etc/sysctl.conf
sed -i '/net.core.wmem_default/d' /etc/sysctl.conf
sed -i '/net.core.wmem_max/d' /etc/sysctl.conf

echo "
kernel.shmmax = $KERNEL_SHMMAX
kernel.shmall = $KERNEL_SHMALL

fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586">>/etc/sysctl.conf
sysctl -p>&/dev/null
echo -e "内核参数配置完毕！\n"
}

#修改系统资源限制
function user_limit() {
echo -e "开始修改系统用户限制！\n"
sed -i '/oracle/d' /etc/security/limits.conf
echo "
oracle soft nproc 8094
oracle hard nproc 16384
oracle soft nofile 16394
oracle hard nofile 65536
oracle soft stack 10240">>/etc/security/limits.conf
echo -e "系统用户限制修改完毕！\n"
}

#修改session认证要求
function modify_pam_login() {
echo -e "开始修改login！\n"
sed -i '/pam_limits.so/d' /etc/pam.d/login
echo "session    required     pam_limits.so" >> /etc/pam.d/login
echo -e "login修改完毕！\n"
}

#禁用防火墙
function disable_iptables_firewall() {
echo -e "开始修改防火墙配置！\n"
if [ "${CENTOS_VERSION}" -eq 6 ];then
	service iptables stop&>/dev/null
	chkconfig iptables off
elif [ "${CENTOS_VERSION}" -eq 7 ]
	then
		systemctl stop firewalld.service &>/dev/null
		systemctl disable firewalld.service &>/dev/null
else
	echo "sorry，select oracle version only for 11/12"
	exit 99
fi
echo -e "防火墙修改完毕！\n"
}

#禁用selinux
function disable_selinux() {
sed -i 's/enforcing/disabled/' /etc/selinux/config
echo -e "SElinux修改完毕！\n"
}

#修改/etc/hosts文件
function modify_hosts() {
cat /etc/hosts | grep ${SERVER_IP} | awk '{print $2}' > /tmp/temp_hosts_file.txt
HOSTS_FILE=$(cat /tmp/temp_hosts_file.txt | grep "$HOSTNAME")
if [ -z "$HOSTS_FILE" ]
  then
	echo "$SERVER_IP $HOSTNAME" >> /etc/hosts
fi
echo -e "hosts文件修改完毕！\n"
}

#创建oracle的安装用户
function create_oracle_osuser() {
echo -e "开始创建${ORACLE_USERNAME}用户\n"
groupadd oinstall&>/dev/null
groupadd dba&>/dev/null
useradd -g oinstall -G dba $ORACLE_USERNAME&>/dev/null
echo "$ORACLE_PASSWD" | passwd  --stdin "$ORACLE_USERNAME"&>/dev/null
echo -e "${ORACLE_USERNAME}用户创建完毕！\n"
}

#设置用户的环境变量
function set_user_env_var_11g() {
ORACLE_HOME=${ORACLE_BASE}/product/11.2.0/dbhome_1
su - ${ORACLE_USERNAME}<<EOF
echo "export PATH
export TMP=/tmp
export ORACLE_BASE=${ORACLE_BASE}
export ORACLE_HOME=${ORACLE_BASE}/product/11.2.0/dbhome_1
export ORACLE_SID=${ORACLE_SID}
export PATH=${ORACLE_HOME}/bin:/usr/sbin:${ORACLE_HOME}/OPatch:${PATH}
export LD_LIBRARY_PATH=${ORACLE_HOME}/lib:/lib:/usr/lib
export CLASSPATH=${ORACLE_HOME}/JRE:${ORACLE_HOME}/jlib:${ORACLE_HOME}/rdbms/jlib
export NLS_LANG=AMERICAN_AMERICA.UTF8
export LC_ALL=en_US.UTF-8">>.bash_profile
source .bash_profile &>/dev/null
exit
EOF
}

#设置用户的环境变量
function set_user_env_var_12c() {
ORACLE_HOME=${ORACLE_BASE}/product/12.2.0/dbhome_1
su - ${ORACLE_USERNAME}<<EOF
echo "export PATH
export TMP=/tmp
export ORACLE_BASE=${ORACLE_BASE}
export ORACLE_HOME=${ORACLE_BASE}/product/12.2.0/dbhome_1
export ORACLE_SID=${ORACLE_SID}
export PATH=${ORACLE_HOME}/bin:/usr/sbin:${ORACLE_HOME}/OPatch:${PATH}
export LD_LIBRARY_PATH=${ORACLE_HOME}/lib:/lib:/usr/lib
export CLASSPATH=${ORACLE_HOME}/JRE:${ORACLE_HOME}/jlib:${ORACLE_HOME}/rdbms/jlib
export NLS_LANG=AMERICAN_AMERICA.UTF8
export LC_ALL=en_US.UTF-8">>.bash_profile
source .bash_profile &>/dev/null
exit
EOF
}

#设置用户的环境变量
function set_user_env_var() {
echo -e "开始配置${ORACLE_USERNAME}用户环境变量！\n"
	if [ "${ORACLE_VERSION}" -eq 11 ];then
		set_user_env_var_11g
	elif [ "${ORACLE_VERSION}" -eq 12 ]
		then
			set_user_env_var_12c
	else
		echo "sorry，select oracle version only for 11/12"
		exit 99
	fi
echo -e "${ORACLE_USERNAME}用户环境变量配置完毕！\n"
}

#准备oracle的安装文件
function ready_install_file() {
echo -e "开始准备安装文件！\n"
if [ -d "${SOFTWARE_DIR}/database" ]
then
	rm -fr ${SOFTWARE_DIR}/database
fi
cd ${SOFTWARE_DIR};ls *.zip&>/dev/null && echo $(ls *.zip)>${SOFTWARE_DIR}/temp_log.file
if [ -e ${SOFTWARE_DIR}/temp_log.file ]
then
for i in $(cat ${SOFTWARE_DIR}/temp_log.file)
do
    if [ $(ls -l $i | awk '{print $5}') -gt 1024000000 ]
    then
        unzip $i&>/dev/null
    fi
done
fi
ls *.tgz&>/dev/null && echo $(ls *.tgz)>${SOFTWARE_DIR}/temp_log.file
if [ -e ${SOFTWARE_DIR}/temp_log.file ]
then
for i in $(cat ${SOFTWARE_DIR}/temp_log.file)
do
	if [ $(ls -l $i | awk '{print $5}') -gt 1024000000 ]
    then
        tar -zxvf $i&>/dev/null
    fi
done
fi
rm ${SOFTWARE_DIR}/temp_log.file
chmod -R 755 database
if [ ! -d $ORACLE_BASE ]
then
mkdir -p ${ORACLE_BASE}
fi
INSTALL_DIR=$(echo ${ORACLE_BASE} | cut -d "/" -f 2)
chmod -R 755 /${INSTALL_DIR}
chown -R ${ORACLE_USERNAME}:oinstall /${INSTALL_DIR}
chown -R ${ORACLE_USERNAME}:oinstall ${SOFTWARE_DIR};
}

#添加swap
function add_swap() { 
random_number=${RANDOM}
dd  if=/dev/zero  of=/var/swapfile_${random_number}  bs=1024  count=200k
mkswap -f  /var/swapfile_${random_number}  
swapon   /var/swapfile_${random_number}
chmod -R 600 /var/swapfile_${random_number}
echo  "/var/swapfile_${random_number}   swap  swap  defaults  0  0" >>  /etc/fstab
}

#检查swap
function check_swap() {
if [ -z $(free -m | grep -i "swap" | awk '{print $2}') ]; then
        add_swap
elif [ $(free -m | grep -i "swap" | awk '{print $2}') -lt 200 ]
        then
                add_swap
fi
}

#静默安装11g数据库
function silent_install_oracle_11g() {
sed -i -r 's#INVENTORY_LOCATION=/u01/app/oraInventory#INVENTORY_LOCATION='${ORACLE_BASE}'/oraInventory#' ${SOFTWARE_DIR}/11g_db_install.rsp;
sed -i -r 's#ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1#ORACLE_HOME='${ORACLE_BASE}'/product/11.2.0/dbhome_1#' ${SOFTWARE_DIR}/11g_db_install.rsp;
sed -i -r 's#ORACLE_BASE=/u01/app/oracle#ORACLE_BASE='${ORACLE_BASE}'#' ${SOFTWARE_DIR}/11g_db_install.rsp;
su - $ORACLE_USERNAME<<EOF
cd ${SOFTWARE_DIR}/database
nohup ./runInstaller -silent -responseFile ${SOFTWARE_DIR}/11g_db_install.rsp &>/home/$ORACLE_USERNAME/oracle_install.log
exit
#重定向到安装日志文件，用函数检查，检查到Successfully Setup Software.时退出。
EOF
}

#静默安装12c数据库
function silent_install_oracle_12c() {
sed -i -r 's#INVENTORY_LOCATION=/u01/app/oraInventory#INVENTORY_LOCATION='${ORACLE_BASE}'/oraInventory#' ${SOFTWARE_DIR}/12c_db_install.rsp;
sed -i -r 's#ORACLE_HOME=/u01/app/oracle/product/12.2.0/dbhome_1#ORACLE_HOME='${ORACLE_BASE}'/product/12.2.0/dbhome_1#' ${SOFTWARE_DIR}/12c_db_install.rsp;
sed -i -r 's#ORACLE_BASE=/u01/app/oracle#ORACLE_BASE='${ORACLE_BASE}'#' ${SOFTWARE_DIR}/12c_db_install.rsp;
su - $ORACLE_USERNAME<<EOF
cd ${SOFTWARE_DIR}/database
nohup ./runInstaller -silent -responseFile ${SOFTWARE_DIR}/12c_db_install.rsp &>/home/$ORACLE_USERNAME/oracle_install.log
exit
#重定向到安装日志文件，用函数检查，检查到Successfully Setup Software.时退出。
EOF
}

function start_install_oracle() {
	echo -e "开始数据库安装！--详细安装过程请 tail -f /home/${ORACLE_USERNAME}/oracle_install.log \n"
	check_swap &>/dev/null
	if [ "${ORACLE_VERSION}" -eq 11 ];then
		silent_install_oracle_11g &>/dev/null
	elif [ "${ORACLE_VERSION}" -eq 12 ]
		then
			silent_install_oracle_12c &>/dev/null
	else
		echo "sorry，select oracle version only for 11/12"
		exit 99
	fi
}

#执行修正脚本
function exec_fix_scripts() {
while [ true ]
EXECU_FIXED_TEXT=$(cat /home/$ORACLE_USERNAME/oracle_install.log | grep "Successfully Setup Software")
echo $EXECU_FIXED_TEXT&>/dev/null
do
	if [ -n "$EXECU_FIXED_TEXT" ]
		then
			break
	fi
	printf .
	sleep 5
done
printf OK
echo " " 
#sh ${ORACLE_HOME}/root.sh &>/dev/null
#sh $(find / -name orainstRoot.sh) &>/dev/null
#sh $(cat /home/${ORACLE_USERNAME}/.bash_profile | grep "ORACLE_HOME" | cut -d "=" -f 2)/root.sh &>/dev/null
sh ${ORACLE_HOME}/root.sh&>/dev/null
sh $(find / -name orainstRoot.sh) &>/dev/null
rm -fr ${SOFTWARE_DIR}/database
echo -e "\n数据库安装完毕！\n"
}

#检查oracle安装进度
function check_install_progress() {
while [ true ]
INSTANCE_TEMP_FILE=$(cat /home/${ORACLE_USERNAME}/create_instance.log | grep "100%")
echo $INSTANCE_TEMP_FILE&>/dev/null
do
	if [ -n  "$INSTANCE_TEMP_FILE" ]
		then
			break
	fi
	printf .
	sleep 5
done
printf OK 
echo " " 
rm -fr /tmp/sh-thd*
rm -fr /tmp/OraInstall*
echo -e "数据库安装完成！\n"
}

#添加tmpfs
function add_tmpfs_2g() {
sed -i '/tmpfs/d' /etc/fstab
echo "tmpfs                   /dev/shm                tmpfs   defaults,size=2G        0 0" >> /etc/fstab
umount /dev/shm/
mount /dev/shm/
}

#检查tmpfs size
function check_tmpfs_size() {
if [ $(df -m | grep tmpfs | awk '{print $2}' |wc -l) -eq 0 ];then
  add_tmpfs_2g
elif [ $(df -m | grep tmpfs | awk '{print $2}' |wc -l) > 1 ];then
  sum=0
  for i in $(df -m | grep tmpfs | awk '{print $2}')
  do
    sum=$(($sum+$i))
  done
  if [ $sum -lt 2048 ];then
	add_tmpfs_2g
  fi  
else
  COMPARE_NUMBER=$(echo "$(df -m | grep tmpfs | awk '{print $2}' ) < 2048" | bc)
  if [ "$COMPARE_NUMBER" -eq 1 ];then
    add_tmpfs_2g
  fi
fi
}

#创建11g实例
function create_11g_instance() {
sed -i -r 's#<initParam name="db_name" value=""/>#<initParam name="db_name" value="'${GLOBAL_DB_NAME}'"/>#' ${SOFTWARE_DIR}/11g_instance_create.dbt;
sed -i -r 's#<initParam name="pga_aggregate_target" value="" unit="MB"/>#<initParam name="pga_aggregate_target" value="'${INSTANCE_PGA}'" unit="MB"/>#' ${SOFTWARE_DIR}/11g_instance_create.dbt;
sed -i -r 's#<initParam name="sga_target" value="" unit="MB"/>#<initParam name="sga_target" value="'${INSTANCE_SGA}'" unit="MB"/>#' ${SOFTWARE_DIR}/11g_instance_create.dbt;
su - ${ORACLE_USERNAME}<<EOF
nohup dbca -silent -createDatabase -templateName ${SOFTWARE_DIR}/11g_instance_create.dbt -gdbname ${GLOBAL_DB_NAME} -sid ${ORACLE_SID} -sysPassword Hand1234 -systemPassword Hand1234 &>/home/${ORACLE_USERNAME}/create_instance.log
exit
EOF
}

#创建12c实例
function create_12c_instance() {
sed -i -r 's#<initParam name="db_name" value=""/>#<initParam name="db_name" value="'${GLOBAL_DB_NAME}'"/>#' ${SOFTWARE_DIR}/12c_instance_create.dbt;
sed -i -r 's#<initParam name="pga_aggregate_target" value="" unit="MB"/>#<initParam name="pga_aggregate_target" value="'${INSTANCE_PGA}'" unit="MB"/>#' ${SOFTWARE_DIR}/12c_instance_create.dbt;
sed -i -r 's#<initParam name="sga_target" value="" unit="MB"/>#<initParam name="sga_target" value="'${INSTANCE_SGA}'" unit="MB"/>#' ${SOFTWARE_DIR}/12c_instance_create.dbt;
su - ${ORACLE_USERNAME}<<EOF
nohup dbca -silent -createDatabase -templateName ${SOFTWARE_DIR}/12c_instance_create.dbt -gdbname ${GLOBAL_DB_NAME} -sid ${ORACLE_SID} -sysPassword Hand1234 -systemPassword Hand1234 -createAsContainerDatabase true &>/home/${ORACLE_USERNAME}/create_instance.log
exit
EOF
}

#创建实例
function start_create_instance() {
check_tmpfs_size
echo -e "开始创建数据库实例！--详细创建过程请 tail -f /home/${ORACLE_USERNAME}/create_instance.log \n"
if [ "${ORACLE_VERSION}" -eq 11 ];then
	create_11g_instance &>/dev/null
elif [ "${ORACLE_VERSION}" -eq 12 ]
	then
		create_12c_instance &>/dev/null
else
	echo "sorry，select oracle version only for 11/12"
	exit 99
fi
check_install_progress
}

#添加监听文件
function new_11g_listener_file() {
DB_NAME=$(cat ${SOFTWARE_DIR}/11g_instance_create.dbt | grep "db_name" | cut -d "=" -f 3 | cut -d "\"" -f 2)
echo "
LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = ${HOSTNAME})(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

ADR_BASE_LISTENER = ${ORACLE_BASE}" > ${ORACLE_HOME}/network/admin/listener.ora
}

#添加监听文件
function new_12c_listener_file() {
DB_NAME=$(cat ${SOFTWARE_DIR}/12c_instance_create.dbt | grep "db_name" | cut -d "=" -f 3 | cut -d "\"" -f 2)
echo "
LISTENER =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = ${HOSTNAME})(PORT = 1521))
    )
SID_LIST_LISTENER=
(SID_LIST=
  (SID_DESC =
  (GLOBAL_DBNAME = ${DB_NAME})
  (SID_NAME = ${ORACLE_SID})
  )

  (SID_DESC =
  (GLOBAL_NAME = pdbxxx)
  (SID_NAME = ${ORACLE_SID})
  )
)

ADR_BASE_LISTENER = ${ORACLE_BASE}" > ${ORACLE_HOME}/network/admin/listener.ora
}

#启动监听
function start_listener() {
su - ${ORACLE_USERNAME}<<EOF
lsnrctl start&>/dev/null
exit
EOF
chown ${ORACLE_USERNAME}:oinstall ${ORACLE_HOME}/network/admin/listener.ora
}

#添加监听文件
function add_listener() {
echo -e "开始配置监听器！\n"
ORACLE_HOME=$(cat /home/${ORACLE_USERNAME}/.bash_profile | grep "ORACLE_HOME" | cut -d "=" -f 2)
if [ "${ORACLE_VERSION}" -eq 11 ];then
	new_11g_listener_file
elif [ "${ORACLE_VERSION}" -eq 12 ]
	then
		new_12c_listener_file
else
	echo "sorry，select oracle version only for 11/12"
	exit 99
fi
start_listener &>/dev/null
echo -e "监听器配置完毕！\n"
}

#添加tns文件
function new_11g_tns_file() {
echo "
${DB_NAME} =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = ${HOSTNAME})(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ${DB_NAME})
    )
  )" > ${ORACLE_HOME}/network/admin/tnsnames.ora
}

#添加tns文件
function new_12c_tns_file() {
echo "
${DB_NAME} =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = ${HOSTNAME})(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ${DB_NAME})
    )
  )

pdbxxx =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = ${HOSTNAME})(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pdbxx)
    )
  )" > ${ORACLE_HOME}/network/admin/tnsnames.ora
}

#配置tns
function add_tns() {
echo -e "开始配置TNS！\n"
if [ "${ORACLE_VERSION}" -eq 11 ];then
	new_11g_tns_file
elif [ "${ORACLE_VERSION}" -eq 12 ]
	then
		new_12c_tns_file
else
	echo "sorry，select oracle version only for 11/12"
	exit 99
fi
chown ${ORACLE_USERNAME}:oinstall ${ORACLE_HOME}/network/admin/tnsnames.ora
echo -e "数据库TNS配置完毕！\n"
}

#配置Oracle的开机自启动
function oracle_auto_start() {
echo -e "开始配置数据库的开机自动启动！\n"
#sed -i -r -e 's#ORACLE_HOME_LISTNER=\$1#ORACLE_HOME_LISTNER='${ORACLE_HOME}'#' ${ORACLE_HOME}/bin/dbstart
#sed -i -r -e 's#ORACLE_HOME_LISTNER=\$1#ORACLE_HOME_LISTNER='${ORACLE_HOME}'#' ${ORACLE_HOME}/bin/dbshut
sed -i "s/N/Y/" /etc/oratab
#echo "su - oracle -lc ${ORACLE_HOME}/bin/dbstart
#su - oracle -lc \"${ORACLE_HOME}/bin/lsnrctl start\"" >>/etc/rc.d/rc.local
sed -i -r 's#ORA_HOME=/xxx#ORA_HOME='${ORACLE_HOME}'#' ${SOFTWARE_DIR}/start_stop_restart_oracle.sh;
sed -i -r 's#ORA_OWNER=xxx#ORA_OWNER='${ORACLE_USERNAME}'#' ${SOFTWARE_DIR}/start_stop_restart_oracle.sh;
cp ${SOFTWARE_DIR}/start_stop_restart_oracle.sh /etc/rc.d/init.d/oracle
chmod 755 /etc/rc.d/init.d/oracle
chkconfig --add oracle
echo -e "数据库开机自动启动配置完毕！\n"
}

#安装完成提示
function last_propmt_11g() {
echo -e "************************    oracle    all complete    ************************\n"
}

#安装完成提示
function last_propmt_12c() {
last_propmt_11g
echo -e "还需手动操作：！\n"
echo -e "1 ：创建pdb \n"
echo -e "2 ：根据pdb_name修改一下listener.ora文件参数pdbxxx \n"
echo -e "3 ：根据pdb_name修改一下tnsnames.ora文件参数pdbxxx \n"
echo -e "
##############################################################################
\n"
}

function last_propmt() {
	if [ "${ORACLE_VERSION}" -eq 11 ];then
		last_propmt_11g
	elif [ "${ORACLE_VERSION}" -eq 12 ]
		then
			last_propmt_12c
	else
		echo "sorry，select oracle version only for 11/12"
		exit 99
	fi
}

function start() {

#0.解压yum源、rsp、dbt文件
ready_yum_rsp_dbt_file

#1.安装oracle的依赖环境（前提是联网）
install_dependent

#2.修改系统的内核参数
modify_kernel_parameter

#3.修改系统对oracle用户的限制
user_limit

#4.修改pam认证模块login文件
modify_pam_login

#5.修改防火墙(iptables或者firewall)配置
disable_iptables_firewall

#6.禁用SElinux
disable_selinux

#7.修改hosts文件配置
modify_hosts

#8.创建oracle用户
create_oracle_osuser

#9.配置oracle用户环境变量 
set_user_env_var &>/dev/null

#10.安装包准备:自动解压>100M的zip和tgz文件
ready_install_file

#11.执行数据库的静默安装
start_install_oracle

#12.执行oracle安装的2个修正脚本
exec_fix_scripts

#13.创建实例
start_create_instance

#14.配置oracle监听器
add_listener

#15.配置TNS
add_tns

#16.配置自动启动
oracle_auto_start

#17.提示最后的用户操作
last_propmt
}
start


exit
